package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.GoodsDao;
import com.shop.cloud.domain.hongfu.Goods;
import com.shop.cloud.domain.hongfu.GoodsCategory;
import com.shop.cloud.domain.hongfu.GoodsImage;
import com.shop.cloud.service.hongfu.IGoodsService;
import com.shop.cloud.util.Page;

/**
 * goods
 */
@Service("goodsService")
public class GoodsService extends BaseService<Goods> implements IGoodsService{
    
    @Autowired
	private GoodsDao goodsDao;
    
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    
    @Autowired
    private GoodsImageService goodsImageService;

	@Override
	public MybatisDao<Goods> getEntityDao() {
		return goodsDao;
	}
	
	
	public Page<Goods> allPage(Goods goods) {
		goods.setPageSize((goods.getPageSize_()==null?20:goods.getPageSize_()));
		long count = goodsDao.countByAllGoods(goods);
		Page<Goods> page = new Page<Goods>(goods.getPageIndex_(), goods.getPageSize_());
		List<Goods> list=goodsDao.findAllGoods(goods);
		List<Goods> list2=new ArrayList<Goods>();
		for (int i = 0; i < list.size(); i++) {
			Goods goods2=list.get(i);
			if (goods2.getParentId()!=0) {
				GoodsCategory goodsCategory=new GoodsCategory();
				goodsCategory.setId(new Long((long)goods2.getParentId()));
				goodsCategory=goodsCategoryService.findOne(goodsCategory);
				goods2.setCategoryName(goodsCategory.getName()+" > "+goods2.getCategoryName());
			}else{
				goods2.setCategoryName(goods2.getCategoryName());
			}
			list2.add(goods2);
		}
		page.setData(list2);
		page.setTotalCount(count);
		return page;
	}
	
	
	public Page<Goods> apiPage(Goods goods) {
		goods.setPageSize((goods.getPageSize_()==null?20:goods.getPageSize_()));
		long count = goodsDao.countByAllGoods2(goods);
		Page<Goods> page = new Page<Goods>(goods.getPageIndex_(), goods.getPageSize_());
		
		List<Goods> list=goodsDao.findAllGoods2(goods);
		List<Goods> list2=new ArrayList<Goods>();	
		for (int i = 0; i < list.size(); i++) {
			Goods goods2=list.get(i);
			
			  GoodsImage goodsImage=new GoodsImage();
			    goodsImage.setGoodsId(goods2.getId());
			    List<GoodsImage> goodsImageList=goodsImageService.find(goodsImage);
			    goods2.setGoodsImages(goodsImageList);
			    
			    
			    if (goods2.getIsSpecialPrice()==1) {
			    	
			    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
					String date = df.format(new Date());
					Timestamp.valueOf(date);
					long now=new Date().getTime();
					if (now>goods2.getSpecialStartTime().getTime() && now<goods2.getSpecialEndTime().getTime() ) {
						goods2.setPrice(goods2.getSpecialPrice());
					}
				}
			    list2.add(goods2);
			    
			    
		}
		
		//对价格进行重新排序
	    if (goods.getIsPrice() !=null) {	
	    	sort(list2,goods.getIsPrice());
		}
		
		page.setData(list2);
		page.setTotalCount(count);
		return page;
	}
	
	/**
	 * 按商品价格排序
	 * @param list  
	 * @param type 1-降序   2-升序
	 */
	public  void sort(List<Goods> list,int type){
		
		 Collections.sort(list, new Comparator<Goods>(){  
			 
		       /*  
		        * int compare(Goods o1, Goods o2) 返回一个基本类型的整型，  
		        * 返回负数表示：o1 小于o2，  
		        * 返回0 表示：o1和o2相等，  
		        * 返回正数表示：o1大于o2。  
		        */  
		       public int compare(Goods good1, Goods good2) {  
		        	
		           if(type==1){
		        		//按价格倒序
			            if(good1.getPrice().floatValue() < good2.getPrice().floatValue()){  
			                return 1;  
			            }  
			            if(good1.getPrice().floatValue() == good2.getPrice().floatValue()){  
			                return 0;  
			            }  
		        	}else{
		        		 ////按价格升序
			            if(good1.getPrice().floatValue() > good2.getPrice().floatValue()){  
			                return 1;  
			            }  
			            if(good1.getPrice().floatValue() == good2.getPrice().floatValue()){  
			                return 0;  
			            }  
		        	}
		           
		            return -1;  
		        }  
		   });   
					
	}
	
    
    
}